package com.cos.web.controller;

import com.cos.entity.User;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.Map;

/**
 * @author: COS
 * @time: 2021/5/19 9:28
 * @description: springMVC 的controller
 *          注解@Controller标识本类为控制器
 *          注解@RequestMapping  设置请求路径的映射
 *          http://localhost:8080/demo/d1
 */
@Controller
@RequestMapping("/demo")
public class DemoController {
    //xxxx/emp?method=show
    //xxxxx/demo/d1
    @RequestMapping("/d1")
    public String demo1(){
        System.out.println("这个是第一个springmvc的Controller");
        //html jsp php
        //逻辑视图名
        //view 视图=>demo.jsp ,视图解析器+逻辑视图名==>视图=>demo.jsp
        //demo
        return "index";
    }
    /**
     *
     * @author: COS
     * @return:
     * @time: 2021/5/19 11:53
     * @description: 获取参数数据,只需要在处理请求的方法中,加入对应参数的数据类型和参数名即可,
     *              注意方法参数列表的变量名要和传入的参数的k值相同才能获取到,否则为null
     *              ,如:xxxx/aaaa?name=李四&age=14===↓
     *              @requestMapping("/aaaa")
     *              public String demo2( String name){
     *              =====>在方法内name就有值为李四
     *              }
     *
     *              */
    @RequestMapping("/d2")
    public String demo2(String name, int age, @DateTimeFormat(pattern = "yyyy-MM-dd") Date bornDate){
        System.out.println("name:"+name);
        System.out.println("age:"+age);
        System.out.println("bornDate:"+bornDate);
        return "demo";
    }
    /**
     *
     * @author: COS
     * @return:
     * @time: 2021/5/19 11:51
     * @description: 获取参数数据,k=v k如果和对象的属性名一致如 ename=张三,user对象中有ename属性,
     *              spring就会自动把ename属性的值封装到对象User里面
     */
    @RequestMapping("/d3")
    public String demo3(User u){
        System.out.println(u);
        return "demo";
    }
    /**
     *
     * @author: COS
     * @return:
     * @time: 2021/5/19 11:51
     * @description: 获取request response session 对象
     */
    @RequestMapping("/d4")
    public String demo4(HttpServletRequest request, HttpServletResponse response, HttpSession session){
        System.out.println(request.getParameter("name"));
        return "demo";
    }
    /**
     *
     * @author: COS
     * @return:
     * @time: 2021/5/19 11:25
     * @description:  @RequestParam 标识后面的参数是传入的数据参数
     *
     * k-v
     */
    @RequestMapping("/d5")
    public String demo4(@RequestParam Map<String,Object> maps){
        System.out.println(maps);
        return "demo";
    }
    /**
     *
     * @author: COS
     * @return:
     * @time: 2021/5/19 11:25
     * @description:  @RequestParam 标识后面的参数是传入的数据参数 , 可以设置属性:
     *                                                              name:设置传入参数的key值
     *                                                              required:默认为true,如果没有提供对应的参数会报400错误,可以重新设置为false
     */
    @RequestMapping("/d6")
    public String demo4(@RequestParam(name = "name",required = false) String ename){
        System.out.println(ename);
        return "demo";
    }
    /**
     *
     * @author: COS
     * @return:
     * @time: 2021/5/19 11:34
     * @description: 在url路径上获取参数  /d7/{ename}===>/d7/张三====>ename=张三
     *                  使用路径参数需要在处理请求的方法的参数上加注解@PathVariable("ename")标识该属性的参数是在url路径上
     */    
    @RequestMapping("/d7/{ename}/64684668{id}41684964")
    public String demo5(@PathVariable("ename") String name,@PathVariable int id){
        System.out.println(name);
        System.out.println(id);
        return "demo";
    }



}
